﻿//  Copyright (c) 2011 Ray Liang (http://www.dotnetage.com)
//  Licensed MIT: http://www.opensource.org/licenses/mit-license.php

using System;
using System.Collections.Generic;
using DNA.IO.Compress;
using System.Xml;
using System.Xml.Serialization;
using System.Web;

namespace DNA.Mvc.Installation
{
    public class ZipInstaller : InstallerBase
    {
        public override string Name
        {
            get { return "unzip"; }
        }

        public override void Execute(InstallationContext context)
        {
            var config = GetConfig<ZipConfiguration>();
            string filename = ExecutionPath + "\\" + config.CompressFile;
            if (!System.IO.File.Exists(filename))
                throw new System.IO.FileNotFoundException(filename);

            using (var zip = new ZipExtract(filename))
            {
                var dest = context.HttpContext.Server.MapPath(config.Destination);

                context.Trace.Warn("Extract files to "+dest);

                zip.ExtractTo(dest + (dest.EndsWith("\\", StringComparison.OrdinalIgnoreCase) ? "" : "\\"));
            }
        }

    }

    [XmlRoot("config"), Serializable]
    public struct ZipConfiguration
    {
        [XmlAttribute("destination")]
        public string Destination;

        [XmlAttribute("src")]
        public string CompressFile;
    }
}
